using UnityEngine;
/// <summary>
/// 子弹技能,
/// </summary>
public class bullet : abstuctSkill
{
    /// <summary>
    /// 射击速度
    /// </summary>
    public float speed = 5;
    /// <summary>
    /// 射程,如果为0则射程无限大,直到碰到墙才消失
    /// </summary>
    public float range = 0;

    private bool isShot = false;
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        if (isShot)
        {
            if (range == 0 || Mathf.Abs(initialTransform.position.x - this.transform.position.x) < range)
            {
                this.transform.position += Vector3.Scale(direction, new Vector3(speed, speed, speed)) * Time.deltaTime;

            }
            else
            {
                Destroy(this.gameObject);
            }
        }
    }
    /// <summary>
    /// 直接释放技能,没有前摇后摇
    /// </summary>
    public void shot()
    {
        gameObject.SetActive(true);
        if (source.transform.localScale.x < 0)
        {
            setDirection(new Vector3(-1, 0, 0));

        }
        else
        {
            setDirection(new Vector3(1, 0, 0));

        }

        isShot = true;
    }
    /// <summary>
    /// 设置技能的释放方向,在释放技能前设置
    /// </summary>
    /// <param name="direction"></param>
    public void setDirection(Vector3 direction)
    {
        this.direction = direction;
        if (direction.x < 0)
        {
            this.GetComponent<SpriteRenderer>().flipX = true;
        }
    }
    /// <summary>
    /// 当技能碰到物体时调用此函数
    /// </summary>
    /// <param name="collision"></param>
    public void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log(" gameObject:" + collision.gameObject.name);
        //如果物体为玩家并且不是技能释放者
        if (source != collision.gameObject && collision.gameObject.tag == "Player")
        {

            collision.gameObject.GetComponent<playerControl>().hit(this.source, basicDamage);
            Destroy(this.gameObject);

        }
        //碰到地面销毁技能
        else if (collision.gameObject.tag == "gnd")
        {
            Destroy(this.gameObject);
        }


    }
    public void OnTriggerStay2D(Collider2D collision)
    {

    }
    public void OnTriggerExit2D(Collider2D collision)
    {

    }
    protected override void onExecute()
    {
        this.initialTransform = source.transform.GetChild(0).transform;
        this.gameObject.transform.position = initialTransform.position;
        shot();

    }

}
